package com.yidu.best.systemManagement.Authority.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yidu.best.systemManagement.Authority.dao.AuthorityDao;
import com.yidu.best.systemManagement.Authority.domain.AuthorityResource;
import com.yidu.best.systemManagement.Authority.domain.AuthorityRole;
import com.yidu.best.systemManagement.Authority.domain.AuthorityRoleResource;
import com.yidu.best.systemManagement.Authority.domain.AuthorityUser;
import com.yidu.best.systemManagement.Authority.domain.AuthorityUserRole;
import com.yidu.best.systemManagement.Authority.service.AuthorityService;
import com.yidu.best.systemManagement.resourceManagement.util.IDUtil;
@Service
public class AuthorityServiceImpl implements AuthorityService {
	@Autowired
	private AuthorityDao authorityDao;

	public AuthorityDao getAuthorityDao() {
		return authorityDao;
	}
	public void setAuthorityDao(AuthorityDao authorityDao) {
		this.authorityDao = authorityDao;
	}
	
	@Override
	public List<AuthorityUser> queryUser() {
		return authorityDao.queryUserWithRole();
	}
	@Override
	public List<AuthorityUser> queryUserWithRole(String userId) {
		return authorityDao.queryUserWithRoleById(userId);
	}
	@Override
	public List<AuthorityRole> selectRoleName() {
		return authorityDao.selectRoleName();
	}
	@Override
	public int setUserRole(AuthorityUserRole authorityUserRole) {
		authorityUserRole.setUser_roleId(IDUtil.getUUID());
		return authorityDao.setUserRole(authorityUserRole);
	}
	@Override
	public int updateRoleName(AuthorityUserRole authorityUserRole) {
		return authorityDao.updateRoleName(authorityUserRole);
	}
	@Override
	public List<AuthorityRole> queryRoleWithResource() {
		return authorityDao.queryRoleWithResource();
	}
	@Override
	public List<AuthorityResource> queryRoleResource(AuthorityResource authorityResource) {
		List<AuthorityResource> resource = authorityDao.queryRoleResource(authorityResource);
		return resource;
	}
	@Override
	public int updateUseable(AuthorityUser authorityUser) {
		return authorityDao.updateUseable(authorityUser);
	}
	@Override
	public List<AuthorityUser> queryUsers(String userName, String roleName, Integer useable) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", userName);
		map.put("roleName", roleName);
		return authorityDao.queryUserWithRoles(map);
	}
	@Override
	public int addRoleByResource(AuthorityRoleResource authorityRoleResource) {
		authorityDao.deleteRoleByResource(authorityRoleResource.getRoleId());
		String[] resourceId = authorityRoleResource.getResourceId().split(",");
		String[] resourceName = authorityRoleResource.getResourceName().split(",");
		int total = 0;
		for (int i = 0; i < resourceId.length; i++) {
			AuthorityRoleResource arr = new AuthorityRoleResource();
			arr.setRoleResId(IDUtil.getUUID());
			arr.setResourceId(resourceId[i]);
			arr.setResourceName(resourceName[i]);
			arr.setRoleId(authorityRoleResource.getRoleId());
			arr.setRoleName(authorityRoleResource.getRoleName());
			int result = authorityDao.addRoleByResource(arr);
			total += result;
		}
		return total == resourceId.length ? 1 : 0;
	}
	
	@Override
	public int deleteRoleByResource(String roleId) {
		return authorityDao.deleteRoleByResource(roleId);
	}
	@Override
	public List<AuthorityResource> selectUserInRoleByResource(String userName) {
		return authorityDao.selectUserInRoleByResource(userName);
	}
}
